package com.yijiaw.service;

import com.yijiaw.dao.PartDAO;
import com.yijiaw.dao.ParttypeDAO;

import com.yijiaw.domain.Part;
import com.yijiaw.domain.Parttype;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for Parttype entities
 * 
 */

@Service("ParttypeService")
@Transactional
public class ParttypeServiceImpl implements ParttypeService {

	/**
	 * DAO injected by Spring that manages Part entities
	 * 
	 */
	@Autowired
	private PartDAO partDAO;

	/**
	 * DAO injected by Spring that manages Parttype entities
	 * 
	 */
	@Autowired
	private ParttypeDAO parttypeDAO;

	/**
	 * Instantiates a new ParttypeServiceImpl.
	 *
	 */
	public ParttypeServiceImpl() {
	}

	/**
	 * Save an existing Part entity
	 * 
	 */
	@Transactional
	public Parttype saveParttypeParts(Integer id, Part related_parts) {
		Parttype parttype = parttypeDAO.findParttypeByPrimaryKey(id, -1, -1);
		Part existingparts = partDAO.findPartByPrimaryKey(related_parts.getId());

		// copy into the existing record to preserve existing relationships
		if (existingparts != null) {
			existingparts.setId(related_parts.getId());
			existingparts.setName(related_parts.getName());
			existingparts.setRemark(related_parts.getRemark());
			existingparts.setImage(related_parts.getImage());
			related_parts = existingparts;
		} else {
			related_parts = partDAO.store(related_parts);
			partDAO.flush();
		}

		related_parts.setProducttype(parttype);
		parttype.getParts().add(related_parts);
		related_parts = partDAO.store(related_parts);
		partDAO.flush();

		parttype = parttypeDAO.store(parttype);
		parttypeDAO.flush();

		return parttype;
	}

	/**
	 * Return a count of all Parttype entity
	 * 
	 */
	@Transactional
	public Integer countParttypes() {
		return ((Long) parttypeDAO.createQuerySingleResult("select count(o) from Parttype o").getSingleResult()).intValue();
	}

	/**
	 * Return all Parttype entity
	 * 
	 */
	@Transactional
	public List<Parttype> findAllParttypes(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Parttype>(parttypeDAO.findAllParttypes(startResult, maxRows));
	}

	/**
	 */
	@Transactional
	public Parttype findParttypeByPrimaryKey(Integer id) {
		return parttypeDAO.findParttypeByPrimaryKey(id);
	}

	/**
	 * Delete an existing Part entity
	 * 
	 */
	@Transactional
	public Parttype deleteParttypeParts(Integer parttype_id, Integer related_parts_id) {
		Part related_parts = partDAO.findPartByPrimaryKey(related_parts_id, -1, -1);

		Parttype parttype = parttypeDAO.findParttypeByPrimaryKey(parttype_id, -1, -1);

		related_parts.setProducttype(null);
		parttype.getParts().remove(related_parts);

		partDAO.remove(related_parts);
		partDAO.flush();

		return parttype;
	}

	/**
	 * Save an existing Parttype entity
	 * 
	 */
	@Transactional
	public void saveParttype(Parttype parttype) {
		Parttype existingParttype = parttypeDAO.findParttypeByPrimaryKey(parttype.getId());

		if (existingParttype != null) {
			if (existingParttype != parttype) {
				existingParttype.setId(parttype.getId());
				existingParttype.setName(parttype.getName());
			}
			parttype = parttypeDAO.store(existingParttype);
		} else {
			parttype = parttypeDAO.store(parttype);
		}
		parttypeDAO.flush();
	}

	/**
	 * Delete an existing Parttype entity
	 * 
	 */
	@Transactional
	public void deleteParttype(Parttype parttype) {
		parttypeDAO.remove(parttype);
		parttypeDAO.flush();
	}

	/**
	 * Load an existing Parttype entity
	 * 
	 */
	@Transactional
	public Set<Parttype> loadParttypes() {
		return parttypeDAO.findAllParttypes();
	}
}
